package com.fzb.forum4j.incp;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.dispatcher.RequestMap;
import org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor;

import com.fzb.forum4j.controller.CommonsAction;
import com.opensymphony.xwork2.ActionInvocation;

public class MyValiInterceptor extends AnnotationValidationInterceptor{


	/**
	 * 过validation拦截器。 并且将操作的数据放在值栈顶部
	 */
	private static final long serialVersionUID = 1L;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		doBeforeInvocation(invocation);
		
		System.out.println("是否通过定义的拦截器  "+ ((CommonsAction)invocation.getAction()).getFieldErrors().isEmpty());
		if(!((CommonsAction)invocation.getAction()).getFieldErrors().isEmpty()){
			return invocation.getProxy().getActionName();
		}
		String str=invocation.invokeActionOnly();
		
		//将数据压入值栈顶部
		invocation.getStack().push(((CommonsAction)invocation.getAction()).getData());
		if(str.equals("details")){
			String cid=((CommonsAction)invocation.getAction()).getQmd().getId()+"";
			String key=((CommonsAction)invocation.getAction()).getQmd().getT().getClass().getSimpleName().toLowerCase();
			if(invocation.getInvocationContext().getApplication().get(key)!=null){
				Map<String,Integer> map=(Map<String, Integer>) invocation.getInvocationContext().getApplication().get(key);
				if(map.get(cid)!=null){
					map.put(cid, map.get(cid)+1);
				}
				else{
					map.put(cid,1);
				}
			}
			else{
				Map<String,Integer> view=new HashMap<String, Integer>();
				view.put(cid, 1);
				invocation.getInvocationContext().getApplication().put(key, view);
				invocation.getInvocationContext().getApplication().put(key+"ViewCount", 1);
			}
			invocation.getInvocationContext().getApplication().put(key+"ViewCount", ((Integer)invocation.getInvocationContext().getApplication().get(key+"ViewCount"))+1);
			RequestMap  request=((RequestMap)invocation.getInvocationContext().getContextMap().get("request"));
			
			String uri=((String) request.get("javax.servlet.forward.request_uri"));
			uri=uri.substring(1,uri.length());
			uri=uri.substring(uri.indexOf("/")+1);
			invocation.getInvocationContext().getContextMap().put("forward", uri+"?"+request.get("javax.servlet.forward.query_string"));
		}
		else if(str.equals("goBack")){
			RequestMap  request=((RequestMap)invocation.getInvocationContext().getContextMap().get("request"));
			String uri=((String) request.get("javax.servlet.forward.request_uri"));
			if(uri!=null)
			{
				uri=uri.substring(1,uri.length());
				uri=uri.substring(uri.indexOf("/")+1);
				Object queryString= request.get("javax.servlet.forward.query_string");
				if(queryString==null){
					invocation.getInvocationContext().getContextMap().put("goBack",uri);
				}
				else{
					invocation.getInvocationContext().getContextMap().put("goBack",uri+"?"+queryString);
				}
				return "goBack";
			}
		}
		if(((CommonsAction)invocation.getAction()).getData().get("404")!=null){
			return "notfound";
		}
		return str;
	}
}
